package com.yanqu.road.server.gameplayer.module.activity.signindraw;

import com.yanqu.road.dao.impl.activity.signindraw.SignInDrawUserDataDaoImpl;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.args.ValueFourParamArgs;
import com.yanqu.road.entity.activity.signindraw.SignInDrawConfig;
import com.yanqu.road.entity.activity.signindraw.SignInDrawUserData;
import com.yanqu.road.entity.activity.signindraw.config.SignInDrawDrawInfo;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.log.LogActivitySignIn;
import com.yanqu.road.entity.log.LogActivitySignInDraw;
import com.yanqu.road.entity.log.LogActivitySignInTimeChange;
import com.yanqu.road.entity.player.UserActivityRealReward;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.pb.activity.SignInDrawProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityModule;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.ActivityRealRewardModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.fanxiuspring.FanXiuSpringMgr;
import com.yanqu.road.server.manger.activity.signindraw.SignInDrawActivityMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.system.CommonMgr;
import com.yanqu.road.server.pb.PlayerPb;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.text.MessageFormat;
import java.util.Date;
import java.util.List;

public class SignInDrawModule extends GeneralModule {

    private SignInDrawUserData userData;

    public SignInDrawModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        SignInDrawConfig config = SignInDrawActivityMgr.getConfig();
        if (config != null && ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            userData = new SignInDrawUserDataDaoImpl().getSignInDrawUserData(config.getActivityInfo().getActivityId(), getUserId());
        }
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        if(userData != null){
            if(userData.isInsertOption()){
                new SignInDrawUserDataDaoImpl().add(userData);
            }else if(userData.isUpdateOption()){
                new SignInDrawUserDataDaoImpl().update(userData);
            }
        }
        return true;
    }

    @Override
    public void afterLogin() {
        if (!SystemOpenMgr.systemOpen(player, eSystemId.SignInDrawActivity.getValue())) {
            return;
        }

        syncConfig();

        initUserData();

        resetOneDay();

        syncUserData();

        //每次设置起始计时
        if(userData != null){
            userData.setOnlineTimingStart((int)player.getUserInfo().getLastLoginTime());
        }
    }

    public void syncConfig() {
        SignInDrawConfig config = SignInDrawActivityMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            return;
        }

        SignInDrawProto.SignInDrawConfigDataSyncMsg.Builder configSync = SignInDrawProto.SignInDrawConfigDataSyncMsg.newBuilder();
        for (SignInDrawDrawInfo drawInfo : config.getDrawInfoList()) {
            configSync.addDraw(SignInDrawActivityPb.buildDrawInfo(drawInfo));
        }
        player.sendPacket(Protocol.U_SIGN_IN_DRAW_SYNC_CONFIG, configSync);
    }

    public void syncUserData() {
        SignInDrawConfig config = SignInDrawActivityMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            return;
        }

        if(SystemOpenMgr.systemOpen(player, eSystemId.SignInDrawActivity.getValue())){
            if(userData == null || userData.getActivityId() != SignInDrawActivityMgr.getActivityId()){
                initUserData();
            }
        }

        if(userData == null){
            return;
        }

        SignInDrawProto.SignInDrawUserDataSyncMsg.Builder syncMsg = SignInDrawProto.SignInDrawUserDataSyncMsg.newBuilder();
        SignInDrawProto.SignInDrawUserDataMsg.Builder buildUserData = SignInDrawActivityPb.buildUserData(userData);
        ActivityRealRewardModule realRewardModule = player.getModule(ActivityRealRewardModule.class);
        List<UserActivityRealReward> list = realRewardModule.getUserRealRewardList(config.getActivityInfo());
        if(list.size() > 0){
            buildUserData.addAllRealReward(realRewardModule.getBuilder(list));
        }
        syncMsg.setUserData(buildUserData);
        player.sendPacket(Protocol.U_SIGN_IN_DRAW_SYNC_USER_DATA, syncMsg);
    }

    public synchronized void initUserData() {
        // 系统未解锁
        if (!SystemOpenMgr.systemOpen(player, eSystemId.SignInDrawActivity.getValue())) {
            return;
        }

        // 没有活动
        if (!ActivityMgr.activityInShowTime(SignInDrawActivityMgr.getActivityInfo()) || SignInDrawActivityMgr.getConfig() == null) {
            return;
        }

        ActivityInfo activityInfo = SignInDrawActivityMgr.getActivityInfo();

        // 已经有数据了
        if (userData != null && userData.getActivityId() == activityInfo.getActivityId()) {
            return;
        }

        SignInDrawConfig config = SignInDrawActivityMgr.getConfig();

        SignInDrawUserData tmpUserData = new SignInDrawUserData();
        tmpUserData.setResetTime(DateHelper.getTodayZeroTime());
        tmpUserData.setActivityId(activityInfo.getActivityId());
        tmpUserData.setUserId(getUserId());
        tmpUserData.setOnlineTimingStart((int)player.getUserInfo().getLastLoginTime());

        //最近活跃度
        if(tmpUserData.getActiveDay() == -1) {
            long lastTime = player.getLastLoginTime();
            if (lastTime == 0) {
                lastTime = player.getUserInfo().getLastLoginTime();
            }
            //注册不能是活动开始后，限制新号
            long registerZeroTime = DateHelper.getZeroTimeStamp(player.getUserInfo().getRegisterTime() * 1000);

            long loginZeroTime = DateHelper.getZeroTimeStamp(lastTime * 1000);
            long activityBeginZeroTime = DateHelper.getZeroTimeStamp(config.getActivityInfo().getBeginShowTime() * 1000);
            if(activityBeginZeroTime >= loginZeroTime && activityBeginZeroTime > registerZeroTime){
                int activeDay = (int)((activityBeginZeroTime - loginZeroTime) / DateHelper.DAY_MILLIONS);
                tmpUserData.setActiveDay(activeDay);
                SignInDrawActivityMgr.getLogger().info("activity {} user {}, last login time {}, activity time {}, active day {}", tmpUserData.getActivityId(),
                        tmpUserData.getUserId(), lastTime, config.getActivityInfo().getBeginShowTime(), activeDay);
            }
        }

        tmpUserData.setInsertOption();

        userData = tmpUserData;

    }

    /**
     * 签到
     */
    public int signInToday() {
        if(userData == null){
            return GameErrorCode.E_SYSTEM_NO_OPEN;
        }

        SignInDrawConfig config = SignInDrawActivityMgr.getConfig();
        if(config == null){
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }

        if(userData.getTodaySign() > 0){
            return GameErrorCode.E_SIGN_IN_DRAW_NO_NEED_HAD_SIGN;
        }

        //第几天
        long gapDay = FanXiuSpringMgr.getDiffDayNum(config.getActivityInfo().getBeginTime() * 1000);
        if(userData.getSignDay() >= gapDay){
            return GameErrorCode.E_SIGN_IN_DRAW_NO_NEED_HAD_SIGN;
        }

        BaseActivityCondition activityCondition = getSignInActivityCondition(gapDay);
        if(activityCondition == null){
            return GameErrorCode.E_ACTIVITY_OVER_TIME;
        }
        if(activityCondition.isCompleted(player) || activityCondition.isGetReward()){
            return GameErrorCode.E_SIGN_IN_DRAW_NO_NEED_HAD_SIGN;
        }

        boolean replacePatronsGift = false;
        Property rowProperty = PropertyHelper.parseStringToProperty(activityCondition.getActivityConditionInfo().getRewardStr());
        if(rowProperty.getCountByGoodsId(config.getPATRONS_ID()).compareTo(BigInteger.ZERO) > 0){
            UserPatrons userPatrons = player.getModule(PatronsModule.class).getUserPatrons(config.getPATRONS_ID());
            if(userPatrons != null){
                replacePatronsGift = true;
                //已有门客
                sendMail(config);
                rowProperty.removeProperty(config.getPATRONS_ID());
                rowProperty.addProperty(PropertyHelper.parseStringToProperty(config.getPATRONS_TRANSFORM_ITEM()));
            }
        }

        String reward  = activityCondition.getReward();

        userData.setSignDay(1 + userData.getSignDay());
        userData.setTodaySign(1 + userData.getTodaySign());
        userData.setFreeTimes(config.getSIGNIN_REWARD() + userData.getFreeTimes());

        //日志
        AutoLogMgr.add(new LogActivitySignInTimeChange(userData.getActivityId(),
                userData.getUserId(),
                1,
                config.getSIGNIN_REWARD(),
                userData.getFreeTimes(),
                1));

        player.notifyListener(eGamePlayerEventType.SignInDrawSignInDays.getValue(), new ValueFourParamArgs(BigInteger.ONE, (int)gapDay, 1));

        BaseActivityData baseActivityData = player.getModule(ActivityModule.class).getBaseActivityData(userData.getActivityId());
        baseActivityData.finish(activityCondition.getConditionId());

        if(replacePatronsGift){
            reward = PropertyHelper.parsePropertyToString(rowProperty);
        }

        SignInDrawProto.SignInDrawSingTodayRespMsg.Builder builder = SignInDrawProto.SignInDrawSingTodayRespMsg.newBuilder().setRet(0);
        builder.setUserData(SignInDrawActivityPb.buildUserData(userData));
        builder.setReward(reward);
        player.sendPacket(Protocol.U_SIGN_IN_DRAW_TODAY_SIGN, builder);

        //日志
        AutoLogMgr.add(new LogActivitySignIn(userData.getActivityId(),
                userData.getUserId(),
                1,
                (int)gapDay,
                reward));
        return 0;
    }

    /**
     * 补签
     */
    public int signInBefore(int day) {
        if(userData == null){
            return GameErrorCode.E_SYSTEM_NO_OPEN;
        }

        SignInDrawConfig config = SignInDrawActivityMgr.getConfig();
        if(config == null){
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }

        //第几天
        long gapDay = FanXiuSpringMgr.getDiffDayNum(config.getActivityInfo().getBeginTime() * 1000);
        if(day >= gapDay || userData.getSignDay() >= gapDay){
            return GameErrorCode.E_SIGN_IN_DRAW_NO_NEED_COUNTER_SIGN;
        }

        BaseActivityCondition activityCondition = getSignInActivityCondition(day);
        if(activityCondition == null){
            return GameErrorCode.E_ACTIVITY_OVER_TIME;
        }
        if(activityCondition.isCompleted(player) || activityCondition.isGetReward()){
            return GameErrorCode.E_SIGN_IN_DRAW_NO_NEED_HAD_SIGN;
        }

        //消耗
        Property consume = PropertyHelper.parseStringToProperty(config.getSIGNATURE_CONSUME());
        if(!player.getModule(CurrencyModule.class).removeCurrency(consume, eLogMoneyType.SignInDraw, eLogMoneyType.SignInDrawSignInConsume)){
            return GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
        }

        boolean replacePatronsGift = false;
        Property rowProperty = PropertyHelper.parseStringToProperty(activityCondition.getActivityConditionInfo().getRewardStr());
        if(rowProperty.getCountByGoodsId(config.getPATRONS_ID()).compareTo(BigInteger.ZERO) > 0){
            UserPatrons userPatrons = player.getModule(PatronsModule.class).getUserPatrons(config.getPATRONS_ID());
            if(userPatrons != null){
                replacePatronsGift = true;
                //已有门客
                sendMail(config);
                rowProperty.removeProperty(config.getPATRONS_ID());
                rowProperty.addProperty(PropertyHelper.parseStringToProperty(config.getPATRONS_TRANSFORM_ITEM()));
            }
        }

        player.notifyListener(eGamePlayerEventType.SignInDrawSignInDays.getValue(), new ValueFourParamArgs(BigInteger.ONE, day, 0));
        String reward  = activityCondition.getReward();

        BaseActivityData baseActivityData = player.getModule(ActivityModule.class).getBaseActivityData(userData.getActivityId());
        baseActivityData.finish(activityCondition.getConditionId());

        userData.setSignDay(1 + userData.getSignDay());

        if(replacePatronsGift){
            reward = PropertyHelper.parsePropertyToString(rowProperty);
        }

        SignInDrawProto.SignInDrawSingBeforeRespMsg.Builder builder = SignInDrawProto.SignInDrawSingBeforeRespMsg.newBuilder().setRet(0);
        builder.setUserData(SignInDrawActivityPb.buildUserData(userData));
        builder.setReward(reward);
        player.sendPacket(Protocol.U_SIGN_IN_DRAW_BEFORE_SIGN, builder);

        //日志
        AutoLogMgr.add(new LogActivitySignIn(userData.getActivityId(),
                userData.getUserId(),
                0,
                (int)gapDay,
                reward));
        return 0;
    }

    private void sendMail(SignInDrawConfig config) {
        try {
            List<Integer> rewardList = StringUtils.stringToIntegerList(config.getPATRONS_TRANSFORM_ITEM(), "=");
            String activityName = ServerLanguageMgr.getContent(config.getActivityInfo().getActivityName(), player.getLanguage());
            String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(config.getGOODS_AUTO_CONSUME_TITLE(),
                    player.getLanguage()), activityName);
            GoodsInfo goodsInfo = CommonMgr.getGoodsInfo(rewardList.get(0));
            String goodName;
            if(goodsInfo != null){
                goodName = goodsInfo.getGoodsName();
            }else {
                goodName = MultipleLanguageMgr.getContent("Items-" + rewardList.get(0), player.getLanguage());
            }
            String mailContent = StringUtils.format(ServerLanguageMgr.getContent(config.getGOODS_AUTO_CONSUME_CONTENT(),
                    player.getLanguage()), activityName,
                    MultipleLanguageMgr.getContent("Patrons-" + config.getPATRONS_ID(), player.getLanguage()),
                    goodName,
                    rewardList.get(1) + "");
            MailManager.sendMail(player.getUserId(), 0L, eMailType.SYSTEM.getValue(),
                    config.getPATRONS_TRANSFORM_ITEM(),
                    mailContent,
                    mailTitle,
                    new Date());
        }catch (Exception e){
            SignInDrawActivityMgr.getLogger().info("error : activity {} userId {} reward {}", userData.getActivityId(), userData.getUserId(), config.getPATRONS_TRANSFORM_ITEM());
        }
    }

    /**
     * 分享
     */
    public int share() {
        if(userData == null){
            return GameErrorCode.E_SYSTEM_NO_OPEN;
        }

        SignInDrawConfig config = SignInDrawActivityMgr.getConfig();
        if(config == null){
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }

        if(userData.getTodayShare() == 0){
            userData.setTodayShare(1);
            userData.setFreeTimes(config.getSHARING_REWARD() + userData.getFreeTimes());
            //日志
            AutoLogMgr.add(new LogActivitySignInTimeChange(userData.getActivityId(),
                    userData.getUserId(),
                    1,
                    config.getSHARING_REWARD(),
                    userData.getFreeTimes(),
                    2));
        }

        SignInDrawProto.SignInDrawShareTodayRespMsg.Builder respMsg = SignInDrawProto.SignInDrawShareTodayRespMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.setUserData(SignInDrawActivityPb.buildUserData(userData));
        player.sendPacket(Protocol.U_SIGN_IN_DRAW_SHARE, respMsg);
        return 0;
    }

    /**
     * 抽奖
     */
    public int draw() {
        if(userData == null){
            return GameErrorCode.E_SYSTEM_NO_OPEN;
        }

        SignInDrawConfig config = SignInDrawActivityMgr.getConfig();
        if(config == null){
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }

        if(userData.getFreeTimes() <= 0){
            return GameErrorCode.E_SIGN_IN_DRAW_NO_FREE_TIMES;
        }

        //第n天
        int day = (int)getDiffDayNum(config.getActivityInfo().getBeginTime() * 1000);

        List<SignInDrawDrawInfo> dayItemList = config.getSignInDrawDrawInfo(day);

        if(dayItemList.size() == 0){
            return GameErrorCode.E_ERROR_DATA;
        }


        //直接扣掉
        userData.setFreeTimes(userData.getFreeTimes() - 1);

        //日志
        AutoLogMgr.add(new LogActivitySignInTimeChange(userData.getActivityId(),
                userData.getUserId(),
                0,
                1,
                userData.getFreeTimes(),
                3));

        SignInDrawProto.CrossSignInDrawGoReqMsg.Builder crossReq = SignInDrawProto.CrossSignInDrawGoReqMsg.newBuilder();
        crossReq.setActivityId(userData.getActivityId());
        crossReq.setBaseInfo(PlayerPb.parsePlayerBaseTempMsg(player.getUserInfo()));
        crossReq.setActiveDay(userData.getActiveDay());
        crossReq.setUid(player.getUserInfo().getOpenId());
        player.sendPacket(Protocol.C_SIGN_IN_DRAW_DRAW, crossReq);
        return 0;
    }

    public static long getDiffDayNum(long time) {
        long todayZeroTime = DateHelper.getTodayZeroTimeStamp() / 1000;
        long zeroTime = DateHelper.getZeroTimeStamp(time) / 1000;
        return (todayZeroTime - zeroTime) / DateHelper.DAY_SECONDS + 1;
    }

    public int drawFromCross(int ret, int rewardId) {

        if(userData == null){
            return GameErrorCode.E_SYSTEM_NO_OPEN;
        }

        SignInDrawConfig config = SignInDrawActivityMgr.getConfig();
        if(config == null){
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }

        SignInDrawDrawInfo drawDrawInfo = config.getSignInDrawDrawInfoById(rewardId);

        if(drawDrawInfo == null){
            SignInDrawActivityMgr.getLogger().info("activity {} user {} rewardId {} config is null", userData.getActivityId(), userData.getUserId(), rewardId);
            return GameErrorCode.E_ERROR_DATA;
        }

        UserActivityRealReward realReward = null;
        //只有一个
        List<Integer> paramList = StringUtils.stringToIntegerList(drawDrawInfo.getReward(), "=");
        if(GoodsMgr.isActivityRealReward(paramList.get(0))){
            Property userReward = PropertyHelper.parseStringToProperty(userData.getReward());
            if(userReward == null){
                userReward = new Property();
            }
            Property add = PropertyHelper.parseStringToProperty(drawDrawInfo.getReward());
            userReward.addProperty(add);
            userData.setReward(PropertyHelper.parsePropertyToString(userReward));

            //实物奖励、填写地址
            realReward = player.getModule(ActivityRealRewardModule.class).addRealReward(userData.getActivityId(), paramList.get(0), paramList.get(1));
        }else {
            //奖励
            player.getModule(CurrencyModule.class).addCurrency(drawDrawInfo.getReward(), eLogMoneyType.SignInDraw, eLogMoneyType.SignInDrawDrawReward);
        }

        //日志
        AutoLogMgr.add(new LogActivitySignInDraw(userData.getActivityId(),
                userData.getUserId(),
                drawDrawInfo.getReward(),
                userData.getFreeTimes()));

        SignInDrawProto.SignInDrawGoRespMsg.Builder clientMsg = SignInDrawProto.SignInDrawGoRespMsg.newBuilder();
        clientMsg.setRet(ret);
        clientMsg.setReward(drawDrawInfo.getReward());
        clientMsg.setUserData(SignInDrawActivityPb.buildUserData(userData));
        if(realReward != null){
            clientMsg.setNo(realReward.getNo());
        }
        player.sendPacket(Protocol.U_SIGN_IN_DRAW_DRAW, clientMsg);
        return 0;
    }

    /**
     * 领取在线时长奖励
     */
    public int onlineReward() {

        if(userData == null){
            return GameErrorCode.E_SYSTEM_NO_OPEN;
        }

        SignInDrawConfig config = SignInDrawActivityMgr.getConfig();
        if(config == null){
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }

        if(config.getOnlineTime() == 0 || config.getOnlineReward() == 0){
            return GameErrorCode.E_ERROR_DATA;
        }

        if(userData.getTodayOnline() == 1){
            return GameErrorCode.E_SIGN_IN_DRAW_TODAY_ONLINE_GOT;
        }

        //时间是否足够
        if(userData.getTodayOnlineTime() + DateHelper.getCurrentSecond() - userData.getOnlineTimingStart() < userData.getTodayOnlineTime()){
            return GameErrorCode.E_SIGN_IN_DRAW_TODAY_ONLINE_NO_ENOUGH;
        }

        userData.setTodayOnline(1);

        userData.setFreeTimes(config.getOnlineReward() + userData.getFreeTimes());

        //日志
        AutoLogMgr.add(new LogActivitySignInTimeChange(userData.getActivityId(),
                userData.getUserId(),
                1,
                config.getOnlineReward(),
                userData.getFreeTimes(),
                5));

        SignInDrawProto.SignInDrawOnlineTodayRespMsg.Builder respMsg = SignInDrawProto.SignInDrawOnlineTodayRespMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.setUserData(SignInDrawActivityPb.buildUserData(userData));
        player.sendPacket(Protocol.U_SIGN_IN_DRAW_SETTLE_ONLINE, respMsg);

        return 0;
    }

    /**
     * 每次退出结算一次在线时长
     */
    public void offLine() {
        if(userData == null){
            return;
        }

        SignInDrawConfig config = SignInDrawActivityMgr.getConfig();
        if(config == null){
            return;
        }

        if(userData.getTodayOnline() == 1){
            return;
        }

        if(DateHelper.getCurrentSecond() > userData.getOnlineTimingStart()){
            int onlineTime = DateHelper.getCurrentSecond() - userData.getOnlineTimingStart();
            userData.setOnlineTimingStart(DateHelper.getCurrentSecond());
            userData.setTodayOnlineTime(onlineTime + userData.getTodayOnlineTime());
            SignInDrawActivityMgr.getLogger().info("activityId {} userId {} add online time {}",
                    userData.getActivityId(), userData.getUserId(), onlineTime);
        }

    }

    public void resetOneDay(){
        if(userData != null){
            int zeroTime = DateHelper.getTodayZeroTime();
            if(userData.getResetTime() < zeroTime){
                userData.setResetTime(zeroTime);
                userData.setTodayShare(0);
                userData.setTodaySign(0);
                int old = userData.getFreeTimes();
                userData.setFreeTimes(0);
                userData.setTodayOnlineTime(0);
                userData.setTodayOnline(0);

                //过0点情况
                if(player.getUserInfo().getLastLoginTime() < zeroTime){
                    userData.setOnlineTimingStart(zeroTime);
                }

                //日志
                if(old > 0) {
                    AutoLogMgr.add(new LogActivitySignInTimeChange(userData.getActivityId(),
                            userData.getUserId(),
                            0,
                            old,
                            userData.getFreeTimes(),
                            4));
                }
            }
        }
    }

    public BaseActivityCondition getSignInActivityCondition(long gapDay) {
        BaseActivityData baseActivityData = player.getModule(ActivityModule.class).getBaseActivityData(userData.getActivityId());
        BaseActivityCondition activityCondition = null;
        if(baseActivityData != null) {
            for (BaseActivityCondition baseActivityCondition : baseActivityData.getBaseActivityConditionList()) {
                if (baseActivityCondition.getConditionType() == eGamePlayerEventType.SignInDrawSignInDays.getValue()) {
                    if(baseActivityCondition.getActivityConditionInfo().getParamList().get(2).intValue() == gapDay){
                        activityCondition = baseActivityCondition;
                    }
                }
            }
        }
        return activityCondition;
    }

    public SignInDrawUserData getUserData() {
        return userData;
    }
}
